package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JViewport;

import app.Cookbook;
import net.miginfocom.swing.MigLayout;

/********************************************************
 *
 *  Project :  TeamProject
 *  Package :  gui
 *  File    :  Gui.java
 *  Name    :  Tyler Daines
 *  Date    :  Nov 3, 2013
 *
 ********************************************************/
public class Gui extends JFrame implements ActionListener, MouseListener
{	
	private JMenuBar menuBar;
	private JMenu file;
	private JMenuItem newMeal, newRecipe, newSearch, exit, help, saveAs;
	
	private JPanel buttonPanel;
	
	private JButton home, addRecipe, mealsButton, searchRecipe, timerButton;
	private static int BUTTON_WIDTH = 150, BUTTON_HEIGHT = 50;
	
	public static JTabbedPane tabbedPane;
	
	public static HomePanel homePanel;
	public static MealBundlePanel bundlePanel;

	private Cookbook cookbook;
	private JFileChooser fileChooser;
	
	/********************************************************
	 * Name       : Gui
	 * Purpose    : Constructor
	 * Parameters : 
	 ********************************************************/
	public Gui(int xSize, int ySize)
	{
		super("Recipe Box");
		setSize(xSize, ySize);
		setLayout(new BorderLayout());
		setVisible(true);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		cookbook = new Cookbook();
		fileChooser = new JFileChooser();
		
		initMenuBar();
		initPanels();
		initButtons();
		initTabs();
	}

	/********************************************************
	* Name       : initTabs
	* Purpose    : 
	* Parameters : 
	* Returns    : void
	********************************************************/
	private void initTabs()
	{
		tabbedPane = new JTabbedPane();
		tabbedPane.setLocation(0, 0);
		add(tabbedPane);
		
		createStaticTab(homePanel);
		createStaticTab(bundlePanel);
		tabbedPane.addMouseListener(this);
	}

	/********************************************************
	* Name       : createTab
	* Purpose    : Method for creating a tab.
	* Parameters : JPanel jPanel, String name
	* Returns    : void
	********************************************************/
	public static void createTab(JPanel jPanel)
	{
		final JPanel tabTitleBar = new JPanel();
		JScrollPane tabBody = new JScrollPane();
		tabBody.setViewportView(jPanel);
		
		
		JButton closeButton = new JButton("x");
		closeButton.setBorder(null);
		
		JLabel tabTitle = new JLabel(jPanel.getName());
		
		tabTitleBar.add(tabTitle);
		tabTitleBar.add(closeButton);
		tabTitleBar.setOpaque(false);
		tabTitleBar.setBackground(new Color(0,0,0));
		
		tabbedPane.addTab(jPanel.getName(),tabBody);
		int tabIndex = tabbedPane.getTabCount()-1;

		tabbedPane.setTabComponentAt(tabIndex, tabTitleBar);
		closeButton.addActionListener(new ActionListener()
		{
			
			@Override
			public void actionPerformed(ActionEvent e)
			{
				int index = tabbedPane.indexOfTabComponent(tabTitleBar);
				if(index != -1)
				{
					tabbedPane.remove(index);
				}
				
			}
		});
		
		tabbedPane.setSelectedIndex(tabbedPane.getTabCount()-1);
	}
	
	/********************************************************
	* Name       : createStaticTab
	* Purpose    : Method for creating the Home tab.
	* Parameters : JPanel jPanel, String name
	* Returns    : void
	********************************************************/
	public static void createStaticTab(JPanel jPanel)
	{
		final JPanel tabTitleBar = new JPanel();
		JScrollPane tabBody = new JScrollPane(jPanel);
		
		JLabel tabTitle = new JLabel(jPanel.getName());
		
		tabTitleBar.add(tabTitle);
		tabTitleBar.setOpaque(false);
		tabTitleBar.setBackground(new Color(0,0,0));
		
		tabbedPane.addTab(jPanel.getName(),tabBody);	
		
		
	}
	

	/********************************************************
	* Name       : initPanels
	* Purpose    : 
	* Parameters : 
	* Returns    : void
	********************************************************/
	private void initPanels()
	{
		buttonPanel = new JPanel();
		buttonPanel.setLayout(new MigLayout());
		buttonPanel.setPreferredSize(new Dimension(BUTTON_WIDTH+10,this.getHeight()));
		add(buttonPanel, BorderLayout.WEST);
		homePanel = new HomePanel();
		bundlePanel = new MealBundlePanel();
		
	}

	/********************************************************
	* Name       : initMenuBar
	* Purpose    : 
	* Parameters : 
	* Returns    : void
	********************************************************/
	private void initMenuBar()
	{
		menuBar = new JMenuBar();
		
		add(menuBar,BorderLayout.NORTH);
		
		file = new JMenu("File");
		menuBar.add(file);
		
		saveAs = new JMenuItem("Save recipe as txt");
		file.add(saveAs);
		saveAs.addActionListener(this);
		
		newRecipe = new JMenuItem("New Recipe");
		file.add(newRecipe);
		newRecipe.addActionListener(this);
		
		newMeal = new JMenuItem("Meals");
		file.add(newMeal);
		newMeal.addActionListener(this);
		
		newSearch = new JMenuItem("Search");
		file.add(newSearch);
		newSearch.addActionListener(this);
		
		help = new JMenuItem("Help");
		file.add(help);
		help.addActionListener(this);
		
		exit = new JMenuItem("Exit");
		file.add(exit);
		exit.addActionListener(this);
		
		
		
		
	}

	/********************************************************
	* Name       : initButtons
	* Purpose    : 
	* Parameters : 
	* Returns    : void
	********************************************************/
	private void initButtons()
	{
		
		home = new JButton("Home");
		home.setMinimumSize(new Dimension(BUTTON_WIDTH,BUTTON_HEIGHT));
		home.addActionListener(this);
		buttonPanel.add(home, "cell 0 0");
		
		mealsButton = new JButton("Meals");
		mealsButton.setMinimumSize(new Dimension(BUTTON_WIDTH,BUTTON_HEIGHT));
		mealsButton.addActionListener(this);
		buttonPanel.add(mealsButton, "cell 0 1");
		
		addRecipe = new JButton("Add Recipe");
		addRecipe.setMinimumSize(new Dimension(BUTTON_WIDTH,BUTTON_HEIGHT));
		addRecipe.addActionListener(this);
		buttonPanel.add(addRecipe, "cell 0 2");
				
		searchRecipe = new JButton("Search Recipes");
		searchRecipe.setMinimumSize(new Dimension(BUTTON_WIDTH,BUTTON_HEIGHT));
		searchRecipe.addActionListener(this);
		buttonPanel.add(searchRecipe, "cell 0 3");
		
		timerButton = new JButton("Timer");
		timerButton.setMinimumSize(new Dimension(BUTTON_WIDTH,BUTTON_HEIGHT));
		timerButton.addActionListener(this);
		buttonPanel.add(timerButton, "cell 0 4");
	}

	@Override
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource() == home)
		{
			tabbedPane.setSelectedIndex(0);
			homePanel.refreshLists();
		}
		
		if((e.getSource() == addRecipe)||(e.getSource() ==newRecipe))
		{
			createTab(new RecipePanel());
		}
		
		if((e.getSource() == mealsButton)||(e.getSource() ==newMeal))
		{
			tabbedPane.setSelectedIndex(1);
		}
		
		if((e.getSource() == searchRecipe)||(e.getSource() ==newSearch))
		{
			createTab(new SearchPanel());
		}
		
		if(e.getSource() == timerButton)
		{
			createTab(new TimerPanel());
		}
		
		if(e.getSource() == saveAs)
		{
			
			JViewport viewport = ((JScrollPane) tabbedPane.getSelectedComponent().getComponentAt(0, 0)).getViewport();
			if (viewport.getView() instanceof RecipePanel)
			{
				int returnValue = fileChooser.showSaveDialog(this);
				if(returnValue == JFileChooser.APPROVE_OPTION)
				{
					RecipePanel recipePanel = (RecipePanel)viewport.getView();
					File file = fileChooser.getSelectedFile();
					cookbook.saveRecipeToTxtFile(recipePanel.getRecipe(), file);
					JOptionPane.showMessageDialog(this, "Recipe saved to: "+file.getPath());
				}
				
			}
			else
			{
				JOptionPane.showMessageDialog(this, "Must be in recipe view.");
			}
			
		}
		
		if(e.getSource() == help)
		{
			createTab(new HelpPanel());
		}
		
		if(e.getSource() == exit)
		{
			System.exit(0);
		}
		
	}

	/* (non-Javadoc)
	 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseClicked(MouseEvent e)
	{
		if(e.getSource() == tabbedPane)
		{
			if(tabbedPane.getSelectedIndex() == 0)
			{
				homePanel.refreshLists();
			}
		}
		
	}

	/* (non-Javadoc)
	 * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseEntered(MouseEvent e)
	{
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseExited(MouseEvent e)
	{
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
	 */
	@Override
	public void mousePressed(MouseEvent e)
	{
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseReleased(MouseEvent e)
	{
		// TODO Auto-generated method stub
		
	}
	
	
}
